//
//  HJGroupMemberContentCell.m
//  KaiChat
//
//  Created by lx on 2023/11/6.
//

#import "HJGroupMemberContentCell.h"
#import "HJCollectionViewCell.h"
#import "HJGroupMemberModel.h"
#import "HJChatGroupModel.h"
#import "QMUIButton.h"
#import "UIImage+GIF.h"
#import "UIImage+ImageEffects.h"

@interface HJGroupSettingMemberCell: HJCollectionViewCell

@property (nonatomic, weak) UIImageView *avatarImageView;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UIImageView *identityImageView;
@property (nonatomic, strong) HJGroupMemberModel *model;


@end

@implementation HJGroupSettingMemberCell


- (void)settingSubView {
    [super settingSubView];
        

    UIImageView *avatarImageView = [UIImageView addToView:self.contentView];
    avatarImageView.addCornerRadius(20);
    avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.centerX.offset(0);
        make.width.height.offset(40);
    }];
    
    UIImageView *identityImageView = [UIImageView addToView:self.contentView];
    identityImageView.hidden = YES;
    [identityImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(avatarImageView);
        make.right.equalTo(avatarImageView).offset(3);
        make.width.height.offset(16);
    }];
 
    UILabel *nameLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentCenter];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(avatarImageView.mas_bottom).offset(4);
    }];
 
    _avatarImageView = avatarImageView;
    _nameLabel = nameLabel;
    _identityImageView = identityImageView;
}

- (void)setModel:(HJGroupMemberModel *)model {
    _model = model;
    
    if ([model.sectionIdentifier isEqualToString:@"administrators"]) {
        _identityImageView.hidden = NO;
    } else {
        _identityImageView.hidden = YES;
    }
    
    if ([model.userId isEqualToString:@"add"] || [model.userId isEqualToString:@"remove"]) {// + 1
        _nameLabel.text = nil;
        _avatarImageView.image = [UIImage imageNamed:model.avatar];
        _identityImageView.hidden = YES;
    } else {
        _nameLabel.text = model.nickname;
        [_avatarImageView sd_setImageWithURL:model.avatar.URL placeholderImage:[UIImage imageNamed:defaultAvatarName]];
        if (model.role == 1) {
            _identityImageView.image = [UIImage imageNamed:@"ownerAvatar_icon"];
        } else if (model.role == 2) {
            _identityImageView.image = [UIImage imageNamed:@"adminAvatar_icon"];
        } else {
            _identityImageView.image = nil;
        }
    }
}

 

@end

@interface HJGroupMemberContentCell()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) QMUIButton *viewMoreButton;
@property (nonatomic, weak) QMUIButton *infoButton;
@property (nonatomic, strong) UIImageView *effectView;

@end


@implementation HJGroupMemberContentCell

- (void)settingSubView {
    [super settingSubView];
    

    self.selectionStyle = UITableViewCellSelectionStyleNone;

    UILabel *titleLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMediumMake(14) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(16);
        make.left.offset(leftRightMargin);
    }];
    
        
    QMUIButton *infoButton = [QMUIButton addToView:self.contentView];
    [infoButton setImage:[UIImage imageNamed:@"info-circle"] forState:UIControlStateNormal];
    infoButton.hidden = YES;
    [infoButton addTarget:self action:@selector(didClickInfoButton) forControlEvents:UIControlEventTouchUpInside];
    [infoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_right).offset(3);
        make.centerY.equalTo(titleLabel);
        make.width.height.offset(20);
    }];
    
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom);
        make.left.right.bottom.offset(0);
    }];
    
    QMUIButton *viewMoreButton = [QMUIButton buttonWithSuperView:self.contentView font:UIFontMake(14) color:UIColor.tj_868391Color title:@"View more"];
    [viewMoreButton setImage:[UIImage imageNamed:@"cell_arrow"] forState:UIControlStateNormal];
    [viewMoreButton setImagePosition:QMUIButtonImagePositionRight];
    [viewMoreButton setSpacingBetweenImageAndTitle:2];
    [viewMoreButton sizeToFit];
    viewMoreButton.hidden = YES;
    [viewMoreButton addTarget:self action:@selector(didClickViewMoreButton) forControlEvents:UIControlEventTouchUpInside];
    CGSize viewMoreButtonSize = viewMoreButton.tj_size;
    [viewMoreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(leftRightMargin);
        make.centerY.equalTo(titleLabel);
        make.width.offset(viewMoreButtonSize.width);
        make.height.offset(32);
    }];

    _viewMoreButton = viewMoreButton;
    _titleLabel = titleLabel;
    _infoButton = infoButton;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HJGroupSettingMemberCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HJGroupSettingMemberCell" forIndexPath:indexPath];
    HJGroupMemberModel *model = self.datas[indexPath.row];
    model.sectionIdentifier = self.model.identifier;
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    HJGroupMemberModel *model = self.datas[indexPath.row];

    if ([self.delegate respondsToSelector:@selector(groupMemberContentCell:didClickItem:)]) {
        [self.delegate groupMemberContentCell:self didClickItem:model];
    }
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = 8;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _flowLayout = flowLayout;
        flowLayout.sectionInset = UIEdgeInsetsMake(14, leftRightMargin, 0, leftRightMargin);
        flowLayout.itemSize = CGSizeMake((KScreenWidth -  leftRightMargin * 2 - flowLayout.minimumLineSpacing * 4)/ 5.0 - 0.5, 70);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:HJGroupSettingMemberCell.class forCellWithReuseIdentifier:@"HJGroupSettingMemberCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.bounces = NO;
        _collectionView.scrollEnabled = NO;
    }
    return _collectionView;
}

- (void)setModel:(HJCellModel *)model {
    _model = model;
    
    _titleLabel.text = model.text;
    _viewMoreButton.hidden = [model.identifier isEqualToString:@"administrators"];
    _infoButton.hidden = !_viewMoreButton.hidden;
    
    NSArray *datas = model.anyObject;
    self.datas = datas;
    [self.collectionView reloadData];
    
    NSInteger role = model.textFieldValue.integerValue;
    if (role == 1 || role == 2 || role == 3) {
        _viewMoreButton.hidden = [model.identifier isEqualToString:@"administrators"];
        _effectView.hidden = YES;
    } else {
        _viewMoreButton.hidden = YES;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(250 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
            if (![model.identifier isEqualToString:@"administrators"]) {
                self.effectView.hidden = NO;
                self.effectView.image = [[HJTools convertViewToImage:self.collectionView] blurImageWithRadius:2];
            } else {
               self -> _effectView.hidden = YES;
            }
        });
    }
}

- (UIImageView *)effectView {
    if (!_effectView) {
        _effectView = [[UIImageView alloc] init];
        [self addSubview:self.effectView];
        [_effectView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.collectionView);
        }];
    }
    return _effectView;
}

- (void)didClickViewMoreButton {
    if ([self.delegate respondsToSelector:@selector(groupMemberContentCellDidClickViewMoreButton:)]) {
        [self.delegate groupMemberContentCellDidClickViewMoreButton:self];
    }
}

- (void)didClickInfoButton {
    if ([self.delegate respondsToSelector:@selector(groupMemberContentCellDidClickInfoButton:)]) {
        [self.delegate groupMemberContentCellDidClickInfoButton:self];
    }
}
 
- (void)dealloc {
    TJLog(@"%s", __func__);
}

@end

 
